
/**
 * Initialize the colorpicker and textbox when loading completed.
 * You cann't retrieve element in the xul file before loading completed.
 */
function ColorPrefInit() {
	
	window.addEventListener ("load", initColorPref, false);
}

/**
 * Initialize the colorpicker and textbox.
 */
function initColorPref() {
	
	var qcs_foreground = document.getElementById("qcs_foreground");
	var qcs_background = document.getElementById("qcs_background");
	var qcs_unvisited = document.getElementById("qcs_unvisited");
	var qcs_visited = document.getElementById("qcs_visited");
	
	qcs_foreground.setAttribute("value", colorItem.foreground);
	qcs_background.setAttribute("value", colorItem.background);
	qcs_unvisited.setAttribute("value", colorItem.anchor);
	qcs_visited.setAttribute("value", colorItem.visited);
}

/**
 * Accept user's input, only if the input is all valid.
 */
function acceptInput() {
	
	var validFlag = true;
	var qcs_foreground = document.getElementById("qcs_foreground");
	var qcs_background = document.getElementById("qcs_background");
	var qcs_unvisited = document.getElementById("qcs_unvisited");
	var qcs_visited = document.getElementById("qcs_visited");
	var colorObj = {foreground:qcs_foreground.getAttribute("value"),
					background:qcs_background.getAttribute("value"),
					anchor:qcs_unvisited.getAttribute("value"),
					visited:qcs_visited.getAttribute("value")};
	
	for (var name in colorObj) {
		if (!validateColorStr(colorObj[name])) validFlag = false;
	}
	if (validFlag) window.opener.changeColorSetting(colorObj, itemIndex);
}

/**
 * Ture if color matchs /^#[0-9a-fA-F]{6}$/, false otherwise.
 */
function validateColorStr(color) {

	var pattern = /^#[0-9a-fA-F]{6}$/;
	if ((color != null) && (typeof(color) == 'string') && pattern.test(color))
		return true;
	else return false;
}

var colorItem = window.arguments[0];
var itemIndex = window.arguments[1];
ColorPrefInit();
